﻿@model GiftCardModel
@using Nop.Core.Domain.Catalog;
@Html.ValidationSummary(false)
@Html.HiddenFor(model => model.Id)

<div id="giftcard-edit">
    <ul>
        <li @Html.RenderSelectedTabIndex(0, GetSelectedTabIndex())>
            @T("Admin.GiftCards.Info")
        </li>
        @if (Model.Id > 0)
        {
            <li @Html.RenderSelectedTabIndex(1, GetSelectedTabIndex())>
                @T("Admin.GiftCards.History")
            </li>
        }
    </ul>
    <div>
        @TabInfo()
    </div>
    @if (Model.Id > 0)
    {
        <div>
            @TabHistory()
        </div>
    }
</div>
<script>
    $(document).ready(function() {
        $("#giftcard-edit").kendoTabStrip({
            animation:  {
                open: {
                    effects: "fadeIn"
                }
            },
            select: tabstrip_on_tab_select
        });
    });
</script>

@{
    //custom tabs
    var eventMessage = new AdminTabStripCreated(this.Html, "giftcard-edit");
    EngineContext.Current.Resolve<IEventPublisher>().Publish(eventMessage);
    foreach (var eventBlock in eventMessage.BlocksToRender)
    {
        @eventBlock
    }
}



@*save selected tab index*@
<input type="hidden" id="selected-tab-index" name="selected-tab-index" value="@(GetSelectedTabIndex())">

@helper TabInfo()
    { <script type="text/javascript">
    $(document).ready(function () {

        $("#@Html.FieldIdFor(model => model.GiftCardTypeId)").change(toggleGiftCardType);
        
        toggleGiftCardType();
    });

    function toggleGiftCardType() {
        var selectedGiftCardTypeId = $("#@Html.FieldIdFor(model => model.GiftCardTypeId)").val();
        if (selectedGiftCardTypeId == @(((int)GiftCardType.Virtual).ToString())) {
            $('#pnlRecipientEmail').show();
            $('#pnlSenderEmail').show();
            $('#pnlNotifyRecipient').show();
        }
        else {
            $('#pnlRecipientEmail').hide();
            $('#pnlSenderEmail').hide();
            $('#pnlNotifyRecipient').hide();
        }
    }
    </script>
    <table class="adminContent">
        <tr>
            <td class="adminTitle">
                @Html.NopLabelFor(model => model.GiftCardTypeId):
            </td>
            <td class="adminData">
                @Html.DropDownListFor(model => model.GiftCardTypeId, ((GiftCardType)Model.GiftCardTypeId).ToSelectList())
                @Html.ValidationMessageFor(model => model.GiftCardTypeId)
            </td>
        </tr>
        @if (Model.PurchasedWithOrderId.HasValue)
        {
            <tr>
                <td class="adminTitle">
                    @Html.NopLabelFor(model => model.PurchasedWithOrderId)
                </td>
                <td class="adminData">
                    <a href="@Url.Action("Edit", "Order", new { Id = Model.PurchasedWithOrderId.Value })">@Model.PurchasedWithOrderId.Value - @T("Admin.Common.View")</a>
                </td>
            </tr>
        }
        <tr>
            <td class="adminTitle">
                @Html.NopLabelFor(model => model.Amount):
            </td>
            <td class="adminData">
                @Html.EditorFor(model => model.Amount) [@Model.PrimaryStoreCurrencyCode]
                @Html.ValidationMessageFor(model => model.Amount)
            </td>
        </tr>
        @if (Model.Id > 0)
        {
            <tr>
                <td class="adminTitle">
                    @Html.NopLabelFor(model => model.RemainingAmountStr):
                </td>
                <td class="adminData">
                    @Model.RemainingAmountStr
                </td>
            </tr>
        }
        <tr>
            <td class="adminTitle">
                @Html.NopLabelFor(model => model.IsGiftCardActivated):
            </td>
            <td class="adminData">
                @Html.EditorFor(model => model.IsGiftCardActivated)
                @Html.ValidationMessageFor(model => model.IsGiftCardActivated)
            </td>
        </tr>
        <tr>
            <td class="adminTitle">
                @Html.NopLabelFor(model => model.GiftCardCouponCode):
            </td>
            <td class="adminData">
                @Html.EditorFor(model => model.GiftCardCouponCode)
                @Html.ValidationMessageFor(model => model.GiftCardCouponCode)
                <input type="button" id="generateCouponCode" class="k-button" value="@T("Admin.GiftCards.Fields.GiftCardCouponCode.Generate")" />
                <script type="text/javascript">
        $(document).ready(function () {
            $('#generateCouponCode').click(function () {
                $.ajax({
                    cache:false,
                    type: "POST",
                    url: "@(Url.Action("GenerateCouponCode", "GiftCard"))",
                    success: function (data) {
                         $('#@Html.FieldIdFor(model => model.GiftCardCouponCode)').val(data.CouponCode);
                    },
                    error:function (xhr, ajaxOptions, thrownError){
                        alert('Failed to generate code.');
                    }  
                });
            });
        });
                </script>
            </td>
        </tr>
        <tr>
            <td class="adminTitle">
                @Html.NopLabelFor(model => model.RecipientName):
            </td>
            <td class="adminData">
                @Html.EditorFor(model => model.RecipientName)
                @Html.ValidationMessageFor(model => model.RecipientName)
            </td>
        </tr>
        <tr id="pnlRecipientEmail">
            <td class="adminTitle">
                @Html.NopLabelFor(model => model.RecipientEmail):
            </td>
            <td class="adminData">
                @Html.EditorFor(model => model.RecipientEmail)
                @Html.ValidationMessageFor(model => model.RecipientEmail)
            </td>
        </tr>
        <tr>
            <td class="adminTitle">
                @Html.NopLabelFor(model => model.SenderName):
            </td>
            <td class="adminData">
                @Html.EditorFor(model => model.SenderName)
                @Html.ValidationMessageFor(model => model.SenderName)
            </td>
        </tr>
        <tr id="pnlSenderEmail">
            <td class="adminTitle">
                @Html.NopLabelFor(model => model.SenderEmail):
            </td>
            <td class="adminData">
                @Html.EditorFor(model => model.SenderEmail)
                @Html.ValidationMessageFor(model => model.SenderEmail)
            </td>
        </tr>
        <tr>
            <td class="adminTitle">
                @Html.NopLabelFor(model => model.Message):
            </td>
            <td class="adminData">
                @Html.TextAreaFor(model => model.Message)
                @Html.ValidationMessageFor(model => model.Message)
            </td>
        </tr>
        @if (Model.Id > 0)
        {
            <tr id="pnlNotifyRecipient">
                <td class="adminTitle">
                    @Html.NopLabelFor(model => model.IsRecipientNotified):
                </td>
                <td class="adminData">
                    @Model.IsRecipientNotified.ToString()
                    <input type="submit" name="notifyRecipient" class="k-button" value="@T("Admin.GiftCards.Fields.IsRecipientNotified.Notify")" />
                </td>
            </tr>
        }
        @if (Model.Id > 0)
        {
            <tr>
                <td class="adminTitle">
                    @Html.NopLabelFor(model => model.CreatedOn):
                </td>
                <td class="adminData">
                    @Html.DisplayFor(model => model.CreatedOn)
                </td>
            </tr>
        }
    </table>
}
@helper TabHistory()
    {
        var defaultGridPageSize = EngineContext.Current.Resolve<Nop.Core.Domain.Common.AdminAreaSettings>().DefaultGridPageSize;
        var gridPageSizes = EngineContext.Current.Resolve<Nop.Core.Domain.Common.AdminAreaSettings>().GridPageSizes;

    <div id="usagehistory-grid"></div>

    <script>
        $(document).ready(function () {
            $("#usagehistory-grid").kendoGrid({
                dataSource: {
                    type: "json",
                    transport: {
                        read: {
                            url: "@Html.Raw(Url.Action("UsageHistoryList", "GiftCard", new { giftCardId = Model.Id }))",
                            type: "POST",
                            dataType: "json"
                        }
                    },
                    schema: {
                        data: "Data",
                        total: "Total",
                        errors: "Errors"
                    },
                    error: function(e) {
                        display_kendoui_grid_error(e);
                        // Cancel the changes
                        this.cancelChanges();
                    },
                    pageSize: @(defaultGridPageSize),
                    serverPaging: true,
                    serverFiltering: true,
                    serverSorting: true
                },
                pageable: {
                    refresh: true,
                    pageSizes: [@(gridPageSizes)]
                },
                editable: {
                    confirmation: false,
                    mode: "inline"
                },
                scrollable: false,
                columns: [{
                    field: "UsedValue",
                    title: "@T("Admin.GiftCards.History.UsedValue")",
                    width: 200
                }, {
                    field: "OrderId",
                    title: "@T("Admin.GiftCards.History.Order")",
                    width: 200,
                    template: '<a href="@Url.Content("~/Admin/Order/Edit/")#=OrderId#">#=OrderId# - @T("Admin.Common.View")</a>'
                }, {
                    field: "CreatedOn",
                    title: "@T("Admin.GiftCards.History.CreatedOn")",
                    width: 200,
                    type: "date",
                    format: "{0:G}"
                }]
            });
        });
    </script>
}